package com.ehl.capture.db;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mchange.v2.c3p0.DataSources;
import java.beans.PropertyVetoException;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;

public class DBPool
{
    private static DBPool dbPool;
    private static ComboPooledDataSource defaultDataSource;
    private ArrayList<String> dbNameList = new ArrayList();
    private static Map<String, ComboPooledDataSource> dataSourceMap = new HashMap();
    private Logger logger = Logger.getLogger(DBPool.class);

    static { dbPool = new DBPool(); }

    public void initPool(String db, Map<String, String> map)
    {
        if (db.equals("ehl"))
            try {
                defaultDataSource = new ComboPooledDataSource();
                defaultDataSource.setJdbcUrl(Config.getValue("jdbcurl"));
                defaultDataSource.setDriverClass("oracle.jdbc.driver.OracleDriver");
                defaultDataSource.setUser(Config.getValue("user"));
                defaultDataSource.setPassword(Config.getValue("password"));

                defaultDataSource.setInitialPoolSize(Integer.parseInt(Config.getValue("initialpoolsize")));

                defaultDataSource.setMinPoolSize(Integer.parseInt(Config.getValue("minpoolsize")));

                defaultDataSource.setMaxPoolSize(Integer.parseInt(Config.getValue("maxpoolsize")));

                defaultDataSource.setMaxIdleTime(Integer.parseInt(Config.getValue("maxidletime")));
            } catch (PropertyVetoException e) {
                throw new RuntimeException(e);
            }
        else
            try
            {
                ComboPooledDataSource dataSource = null;
                dataSource = new ComboPooledDataSource();
                dataSource.setJdbcUrl((String)map.get("jdbcurl"));
                dataSource.setDriverClass("oracle.jdbc.driver.OracleDriver");
                dataSource.setUser((String)map.get("user"));
                dataSource.setPassword((String)map.get("password"));

                dataSource.setInitialPoolSize(Integer.parseInt((String)map.get("initialpoolsize")));

                dataSource.setMinPoolSize(Integer.parseInt((String)map.get("minpoolsize")));

                dataSource.setMaxPoolSize(Integer.parseInt((String)map.get("maxpoolsize")));

                dataSource.setMaxIdleTime(Integer.parseInt((String)map.get("maxidletime")));
                dataSourceMap.put(db, dataSource);
                this.dbNameList.add(db);
            } catch (PropertyVetoException e) {
                throw new RuntimeException(e);
            }
    }

    public static final DBPool getInstance()
    {
        return dbPool;
    }

    public static final Connection getConnection()
            throws SQLException
    {
        return defaultDataSource.getConnection();
    }

    public static final Connection getConnection(String datasource)
    {
        Connection conn = null;
        try {
            conn = ((ComboPooledDataSource)dataSourceMap.get(datasource)).getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    public final boolean closeDataSources()
    {
        this.logger.info("连接池正在关闭");
        try {
            DataSources.destroy(defaultDataSource);
            if (this.dbNameList.size() == 0) {
                return true;
            }
            Object[] db = this.dbNameList.toArray();
            ComboPooledDataSource[] source = new ComboPooledDataSource[db.length];
            for (int i = 0; i < db.length; ++i) {
                source[i] = dataSourceMap.get(db[i]);
            }
            for (int i = 0; i < source.length; ++i)
                DataSources.destroy(source[i]);
        }
        catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public static void main(String[] args) throws SQLException {
        Connection con = null;
        try {
            con = getConnection();
            ResultSet rs = con.createStatement().executeQuery("SELECT * FROM LESOGO_USER");
            while (rs.next()) {
                System.out.println(rs.getObject(1));
                System.out.println(rs.getObject(2));
                System.out.println(rs.getObject(3));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (con != null)
                con.close();
        }
    }
}